package il.technion.cs236369.webserver;

import java.io.IOException;
import java.net.Socket;

import org.apache.http.HttpResponse;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;

/**
 * Handles a timeout event in the ThreadPool.
 * Will return a TimedOut response to the client.
 * @author gil
 *
 */
public class TimeoutHandler implements Runnable {
	private final Socket _socket;

	/**
	 * Creates a new TimeoutHandler
	 * @param insocket The socket the run the task on
	 */
	public TimeoutHandler(Socket insocket) {
		_socket = insocket;
	}

	@Override
	public void run() {
		HttpResponse response = RequestHandler.generateErrorResponse(408, "Request Timeout", "Could not finish handling the request, try again later...");
		DefaultHttpServerConnection inconn = new DefaultHttpServerConnection();
		try {
			inconn.bind(_socket, new BasicHttpParams());
		} catch (IOException e) {
			System.err.println("Unable to bind incoming socket");
			return;
		}
		RequestHandler.sendResponseAndClose(_socket, inconn, response);
	}

}
